@extends('admin.common.main')
@section('cnt')
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-sm-4">
        <ol class="breadcrumb">
            <li>
                <a href="index.html">主页</a>
            </li>
            <li>
                <strong>用户管理</strong>
            </li>
        </ol>
    </div>
</div>
{{-- 消息提示 --}}
@include('admin.common.msg')
<div class="page-container">
    <div>
        <span class="l">
            <a class="btn btn-danger radius" onclick="deleteAll()">
                <i class="Hui-iconfont">&#xe6e2;</i> 批量删除
            </a>
            <a class="btn btn-primary radius" href="{{route('admin.user.create')}}">
                <i class="Hui-iconfont">&#xe600;</i> 添加用户
            </a>
        </span>
    </div>
    <div class="mt-20">
        <table class="table table-hover table-striped">
            <thead>
            <tr>
                <th width="25"><input type="checkbox" name="" value=""></th>
                <th width="80">ID</th>
                <th width="100">真实名</th>
                <th width="100">用户名</th>
                <th width="100">角色名</th>
                <th width="40">性别</th>
                <th width="90">手机</th>
                <th width="150">邮箱</th>
                <th width="130">加入时间</th>
                <th width="70">状态</th>
                <th width="160">操作</th>
            </tr>
            </thead>
            <tbody>
            @foreach($data as $item)
                <tr>
                    <td>
                        @if(auth()->id() != $item->id)
                            @if($item->deleted_at == null)
                                <input type="checkbox" value="{{ $item->id }}" name="id[]">
                            @endif
                        @endif
                    </td>
                    <td>{{ $item->id }}</td>
                    <td>{{ $item->truename }}</td>
                    <td>{{ $item->username }}</td>
                    <td>{{ $item->role->role_name }}</td>
                    <td>{{ $item->sex }}</td>
                    <td>{{ $item->phone }}</td>
                    <td>{{ $item->email }}</td>
                    <td>{{ $item->created_at }}</td>
                    <td ><span >已启用</span></td>
                    <td class="td-manage">
                        @if(auth()->id() != $item->id)
                            @if($item->deleted_at != null)
                                <a href="{{ route('admin.user.reset',['id' => $item->id]) }}"
                                   >还原</a>
                            @else
                                <a href="{{ route('admin.user.role',$item) }}" >分配角色</a>
                                {!! $item->editBut('admin.user.edit') !!}
                                {!! $item->delBut('admin.user.del') !!}
                            @endif
                        @endif
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
        {{-- 分页 --}}
        {{ $data->links() }}
    </div>
</div>
@endsection
@section('js')
<script>
    // 生成一个token crsf
    const _token = "{{ csrf_token() }}";
    // 给删除按钮绑定事件
    $('.delbtn').click(function (evt) {
        // 得到请求的url地址
        let url = $(this).attr('href');
        // 发起一个delete请求
        $.ajax({
            url,
            data: {_token},
            type: 'DELETE',
            dataType: 'json'
        }).then(({status, msg}) => {
            if (status == 0) {
                // 提示插件
                layer.msg(msg, {time: 2000, icon: 1}, () => {
                    // 删除当前行
                    window.location.reload();
                });
            }
        });
        // jquery取消默认事件
        return false;
    });

    function deleteAll() {
        let ids = $("input[name='id[]']:checked");
        let id = [];
        let url = "{{route('admin.user.delAll')}}";
        $.each(ids, function (key, value) {
            id.push($(value).val());
        });

        layer.confirm('是否批量删除这些数据？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            if (id.length > 0) {
                $.ajax({
                    url,
                    data: {id, _token},
                    type: 'DELETE'
                }).then(({status, msg}) => {
                    layer.msg(msg, {time: 2000, icon: 1}, () => {
                        // 删除当前行
                        window.location.reload();
                    });
                });
            }
        });
    }

</script>
@endsection